<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-form">
        <el-form :model="loginForm" @submit.prevent="handleSubmit">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入登录账号"
              class="custom-input"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入登录密码"
              class="custom-input"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              @click="handleSubmit"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import { userLogin } from "@/api/login.js";
import { get_unreviewed } from "@/api/audits.js";
import { useAppStore } from "@/stores/app";
import md5 from "js-md5";

const router = useRouter();
const loading = ref(false);
const loginForm = reactive({
  username: "",
  password: "",
  paswd: "",
});
const appStore = useAppStore();
const handleSubmit = async () => {
  loading.value = true;

  try {
    // 这里添加实际的登录逻辑
    loginForm.paswd = md5(loginForm.password);
    userLogin(loginForm).then((res) => {
      if (res.code == 0) {
        localStorage.setItem("token", res.data.token);
        appStore.setUserInfo(res.data);
        appStore.setUnreadCount(res.unreviewed_count || 0);
        router.push(res.path || "/");
      }
    });
  } catch (error) {
    ElMessage.error("登录失败");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
  background-image: url("@/assets/images/login/login_bg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-box {
  display: flex;
  width: 1300px;
  height: 624px;
  overflow: hidden;
  background: url("@/assets/images/login/login.png");
  background-size: 100% 100%;
}

.login-form {
  position: relative;
  z-index: 1;
  width: 31.1%;
  margin-top: 224px;
  margin-left: 6.4%;
}

.custom-input :deep(.el-input__wrapper) {
  background: transparent;
  border: none;
  box-shadow: none;
  height: 40px;
  font-size: 17px;
  margin-bottom: 6px;
  padding-left: 36px;
}

.custom-input :deep(.el-input__inner) {
  color: white;
}

.custom-input :deep(.el-input__inner::placeholder) {
  color: white;
}

.login-button {
  width: 100%;
  height: 57px;
  border-radius: 4px;
  font-size: 22px;
  background: #0088ff;
  border: none;
  margin-top: 90px;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
}
</style>
